package com.hfzy.ihk.web.linkcall.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;

/**
 * 将参数转化为layui所需数据
 *
 * @Auther: Administrator
 * @Date: 2018/9/3 15:03
 * @Description:
 */
public class LayUiParamUtils {

    private static LayUiParamUtils layUiParamUtils;

    Logger logger = LoggerFactory.getLogger(LayUiParamUtils.class);

    //私有构造方法
    private LayUiParamUtils() {

    }

    //饿汉式
    public static LayUiParamUtils getInstance() {

        if (layUiParamUtils == null) {
            synchronized (LayUiParamUtils.class) {
                if (layUiParamUtils == null) {
                    layUiParamUtils = new LayUiParamUtils();
                }
            }
        }

        return layUiParamUtils;
    }


    /**
     * 功能描述:将page对象封装为layui  table(表格)所需数据
     *
     * @param: [page]
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     * @auther:
     * @date: 2018/9/3
     */
    public Map<String, Object> toLayuiTableMap(Page page) {

        Map<String, Object> resultMap = new HashMap<>();

        if (page != null) {
            resultMap.put("count", page.getTotalElements());
            resultMap.put("msg", "请求参数成功");
            resultMap.put("code", "0");
            resultMap.put("data", page.getContent());
        } else {
            resultMap.put("msg", "暂无数据");
            resultMap.put("code", "0");
            resultMap.put("count", null);
            resultMap.put("data", null);

        }
        return resultMap;
    }

    /**
     * 功能描述:封装jquery搜索下拉框所需参数
     *
     * @param: [param, consumer] param:需要进行遍历封装的list,BiConsumer:具体行为
     * @return: java.util.List<java.util.Map<java.lang.String,java.lang.String>>
     * @auther:
     * @date: 2018/9/9
     */
    public List<Map<String, String>> getJquerySelectParams(List param, BiConsumer<Map<String,String>,Object> consumer) {


        List<Map<String, String>> resultList = new ArrayList<>();

        if (param != null && param.size() > 0) {

            for (Object object : param) {
                Map<String, String> resultMap = new HashMap<>();
                consumer.accept(resultMap, object);
                resultList.add(resultMap);
            }
            return resultList;
        }
        return resultList;
    }

}
